package com.base.starter.config;

import com.base.common.utils.DateUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Date;


public class DateDeserializer extends JsonDeserializer<Date> {

    public final static DateDeserializer instance = new DateDeserializer();

    /**
    * @Description: 将JSON字符串反序列化为Date对象 
    * @Param:
    * @return:
    * @Author: zhou minghao
    * @Date: 15:05 2024/4/17 
    */ 
    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String text = p.getText();
        if (StringUtils.isNotBlank(text)) {
            return DateUtils.tryParse(text);
        }
        return null;
    }
}
